package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Qxxx;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class QxxxForm extends BasePage implements Serializable {
    private GenericManager<Qxxx, Long> qxxxManager;
    private Qxxx qxxx = new Qxxx();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setQxxxManager(GenericManager<Qxxx, Long> qxxxManager) {
        this.qxxxManager = qxxxManager;
    }

    public Qxxx getQxxx() {
        return qxxx;
    }

    public void setQxxx(Qxxx qxxx) {
        this.qxxx = qxxx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        qxxxManager.remove(qxxx.getId());
        addMessage("qxxx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            qxxx = qxxxManager.get(id);
        } else {
            qxxx = new Qxxx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (qxxx.getId() == null || qxxx.getId() == 0);
        qxxx.setJbqk(jbqk); 
        // set username who act this operation, get the user name from httprequest
        qxxx.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        qxxx.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        qxxx.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
      
        qxxxManager.save(qxxx);

        String key = (isNew) ? "qxxx.added" : "qxxx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	// set the id to the next page
            this.id = qxxx.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 